import { defineStore } from "pinia";
import {
  listInventoryPage, //盘点单分页查询
  BatchdeleteInventory, //批量删除盘点单
  DeleteInventory, //删除盘点单
} from "@/utils/api/InventoryManagement/IMcheck/IMcheck";
import { dictlist } from "@/utils/api/dict/dict"; //字典列表
import { useMemberStore } from "@/stores/modules/member";

const tableHeader = [
  "盘点单号",
  "盘点类型",
  "盘点时间",
  "盘点状态",
  "操作人",
  "审批状态",
  "操作",
];
export const useIMinventoryStore = defineStore(
  "IMinventory",
  () => {
    // 定义表头
    const IMinventory_tableHeader = tableHeader;
    //盘点单分页列表
    let Inventorylist: any = [];
    let IMinventory_list = reactive({ Inventorylist });
    //总条数
    let totalCount: any = ref(0);
    // 盘点状态列表
    let StatusList: any = [];
    let inventoryStatusList: any = reactive({ StatusList });
    // 盘点类型列表
    let TypeList: any = [];
    let inventoryTypeList: any = reactive({ TypeList });
    // 选择的盘点单
    let selectInventory: any = [];
    let selectInventoryList: any = reactive({ selectInventory });
    //全选状态
    let allchecked = ref(false);
    //存在选中项
    let exist_checked = ref(false);
    /******************** 公共方法 ***********************/
    //获取盘点单分页列表
    const getListInventoryPage = async (data: any) => {
      const userStore: any = useMemberStore();
      // 调用接口获取数据
      const res = await listInventoryPage({
        pageNum: data.pageNum || 1,
        pageSize: data.pageSize || 10,
        searchParam: data.searchParam || "",
        StartDate: data.StartDate || "",
        EndDate: data.EndDate || "",
        InventoryStatus: data.InventoryStatus || "",
        InventoryType: data.InventoryType || "",
        GodownCode: userStore.GodownCode || "",
        GodownKeeper: userStore.user_id,
      });
      let resdata = res?.data;
      resdata?.list.forEach((item: any) => {
        item.checked = false;
      });
      exist_checked.value = false;
      allchecked.value = false;
      selectInventoryList.selectInventory = [];
      IMinventory_list.Inventorylist = resdata?.list;
      totalCount.value = resdata?.totalCount;
      return resdata;
    };
    //获取盘点状态列表
    const getDictList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "InventoryStatus",
      });
      inventoryStatusList.StatusList = res.data;
      return res.data;
    };
    //获取盘点类型列表
    const getInventoryTypeList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "InventoryType",
      });
      inventoryTypeList.TypeList = res.data;
      return res.data;
    };
    //删除盘点单
    const deleteInventory = async (data: any) => {
      const res = await DeleteInventory(data);
      return res;
    };
    //批量删除盘点单
    const batchDeleteInventory = async (data: any) => {
      const res = await BatchdeleteInventory(data);
      return res;
    };
    //选择全部
    const selectAll = () => {
      let arr: any = [];
      if (
        selectInventoryList.selectInventory.length ==
        IMinventory_list.Inventorylist.length
      ) {
        exist_checked.value = false;
        allchecked.value = false;
        IMinventory_list.Inventorylist.forEach((item: any) => {
          item.checked = false;
        });
        selectInventoryList.selectInventory = [];
      } else {
        IMinventory_list.Inventorylist.forEach((item: any) => {
          item.checked = true;
          arr.push(item.InventoryCode);
        });
        selectInventoryList.selectInventory = arr;
        allchecked.value = true;
      }
      console.log("全选", selectInventoryList.selectInventory);
    };
    //选中单个
    const selectItem = (data: any) => {
      if (data.checked) {
        selectInventoryList.selectInventory.push(data.InventoryCode);
        if (
          selectInventoryList.selectInventory.length ==
          IMinventory_list.Inventorylist.length
        ) {
          exist_checked.value = false;
          allchecked.value = true;
        } else {
          exist_checked.value = true;
          allchecked.value = false;
        }
        console.log("选择", selectInventoryList.selectInventory);
      } else {
        allchecked.value = false;
        exist_checked.value = true;
        const index = selectInventoryList.selectInventory.indexOf(
          data.InventoryCode
        );
        selectInventoryList.selectInventory.splice(index, 1);
        if (selectInventoryList.selectInventory.length == 0) {
          exist_checked.value = false;
          allchecked.value = false;
        }
        console.log("取消选择", selectInventoryList.selectInventory);
      }
    };
    /*********************** 对外暴露 ********************************/
    return {
      IMinventory_tableHeader,
      getListInventoryPage,
      IMinventory_list,
      getDictList,
      getInventoryTypeList,
      inventoryStatusList,
      inventoryTypeList,
      totalCount,
      selectAll,
      selectItem,
      exist_checked,
      allchecked,
      selectInventoryList,
      deleteInventory,
      batchDeleteInventory,
    };
  },
  /**********************************TODO: 持久化***********************/
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
      paths: ["IMinventory_tableHeader", ""],
    },
  }
);
